#include <iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
int dx[4] = { 1,-1,0,0 };
int dy[4] = { 0,0,1,-1 };
bool visited[100][100];
int n, m;

bool dfs(vector<string> board, size_t x, size_t y, string & word, size_t pos) 
{
    if (pos == word.size())    return true;
  
    
    for (int i = 0; i < 4; i++) 
    {
         int next_x = x + dx[i];
         int next_y = y + dy[i];
         if (next_x >=0  && next_x < n && next_y >= 0 && next_y < m
             && board[next_x][next_y] == word[pos]
             && !visited[next_x][next_y] )
         {
             visited[next_x][next_y] = true;
             if (dfs(board, next_x, next_y, word, pos + 1)) return true;
             visited[next_x][next_y] = false;
         }
    }
  
    return false;

   
}
bool exist(vector<string>& board, string word) 
{
   n = board.size(), m = board[0].size();
    bool ret = false;
    for (int i = 0; i < n; i++) 
    {
        for (int j = 0; j < m; j++) 
        {
            if (board[i][j] == word[0])
            {
                visited[i][j] = true;
                if (dfs(board, i, j, word, 1)) return true;
                visited[i][j] = false;
            }
        }
    }
   
    return false;
    
}
//int main() 
//{
//    vector<string> aboard;
//    aboard.push_back("ab");
// 
//    string word =  "ab" ;
//    bool ans = exist(aboard, word);
//    cout << ans;
//
//    return 0;
//}